package org.linlinjava.litemall.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallSettlement;
import org.linlinjava.litemall.db.domain.LitemallSettlementLog;
import org.linlinjava.litemall.db.service.LitemallSettlementLogService;
import org.linlinjava.litemall.db.service.LitemallSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理协议控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/settlement")
@Validated
public class AdminSettlementController {

    /**
     * 结算服务
     */
    @Autowired
    private LitemallSettlementService settlementService;
    /**
     * 解决日志服务
     */
    @Autowired
    private LitemallSettlementLogService settlementLogService;

    /**
     * 列表
     *
     * @param name   名字
     * @param userId 用户id
     * @param shopId 商店id
     * @param page   页面
     * @param limit  限制
     * @param sort   排序
     * @param order  订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String name, Integer userId,
    				   @LoginAdminShopId Integer shopId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallSettlement> list = settlementService.querySelective(shopId, userId, name, page, limit, sort, order);
        return ResponseUtil.okList(list);
    }

    /**
     * 细节
     * 交接详情
     *
     * @param id 交接id
     * @return {@link Object}
     */
    @GetMapping("/detail")
    @LogAnno
    public Object detail(@NotNull @RequestParam(value = "id") Integer id) {
    	LitemallSettlement settlement = settlementService.findById(id);
    	List<LitemallSettlementLog> logs = settlementLogService.queryBySettlementId(id);
    	
    	Map<String, Object> data = new HashMap<>();
        data.put("settlement", settlement);
        data.put("settlementLogs", logs);
    	return ResponseUtil.ok(data);
    }

}
